package org.wtframework.website.web.mvc.action.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.openredhead.springx.core.server.page.IPage;
import org.openredhead.springx.core.server.service.Pages;
import org.openredhead.springx.core.web.mvc.support.ActionValueAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wtframework.platform.server.util.ZykConstant;
import org.wtframework.website.server.manager.NoticeBoardManager;
import org.wtframework.website.server.model.impl.NoticeBoard;
import org.wtframework.website.server.service.NoticeBoardService;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("/back/website")
public class NoticeBoardActionController {
	
	private static String BASE_URL = "admin/back_layui/website/";
	
	@Autowired
	private NoticeBoardManager noticeBoardManager;
	
	@Autowired
	private NoticeBoardService noticeBoardService;
	
	
	@RequestMapping("/noticeManager")
	public String noticeManagerUI(ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		return BASE_URL+"noticeBoard/noticeManager";
	}
	/**
	 * 
	 * <B>方法描述：</B>（这里用一句话描述这个方法的作用） <br/>
	 *
	 * <B>作者：	 </B> 陈长炯 <br/>
	 * <B>创建日期：</B> 2017年9月28日 下午3:58:56 
	 * @param modelMap
	 * @param actionValueAssistant
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/noticeManagerData")
	@ResponseBody
	public Map<String, Object> noticeManager(ModelMap modelMap,ActionValueAssistant actionValueAssistant)throws ParseException{
		Map<String,Object> resultMap = Maps.newHashMap();
		String title = actionValueAssistant.get(String.class, "title");
		Integer pageNo = actionValueAssistant.get(Integer.class,"page");
		Integer pageSize = actionValueAssistant.get(Integer.class, "limit");//layui默认pageSize
		if(pageNo==null){
			pageNo=1;
		}
		String hql = "select id,title,publisher,date_format(submiTime, '%Y-%m-%d %H:%i') ,date_format(pusTime, '%Y-%m-%d %H:%i') ,issueStatus ,isTop from NoticeBoard where 1=1";
		List<Object> valueList = Lists.newArrayList();
		if(!Strings.isNullOrEmpty(title)){
			hql+="and title like ? ";
			valueList.add("%"+title+"%");
		}
		hql+="order by isTop desc";
		IPage<Map<String,Object>> page = noticeBoardManager.findProperty
		(new String[]{"id","title","publisher","submiTime","pusTime","issueStatus","isTop"},hql,valueList.toArray())
		.getPageByPageNo(pageNo, pageSize);
		Integer beginIndex = (pageNo-1)*pageSize;
		for(Map<String,Object> map:page.getData()){
			map.put("index", ++beginIndex);
		}
		resultMap.put("count", page.getTotalCount());
		resultMap.put("data", page.getData());
		resultMap.put("code", 0);
		resultMap.put("msg", "");//当数据出错是输出该句子
		return resultMap;
	}
	
	
	/**
	 * 
	 * <B>方法描述：</B>（公告添加和修改方法） <br/>
	 *
	 * <B>作者：	 </B> 何展操 <br/>
	 * <B>创建日期：</B> 2014-8-20 下午10:24:20 
	 * @param noticeId
	 * @param modelMap
	 * @param actionValueAssistant
	 * @return
	 */
	@RequestMapping("/noticeContent")
	public String noticeAddOrUpdate(ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		Integer noticeId=actionValueAssistant.get(Integer.class,"noticeId");
		if(noticeId!=null){
			NoticeBoard notice=noticeBoardService.findNoticeById(noticeId);
			modelMap.put("noticeBoard", notice);
		}
		return BASE_URL+"noticeBoard/noticeContent";
	}
	
	@RequestMapping("/checkNoticeDetail/{noticeId}")
	public String checkNoticeDetail(@PathVariable Integer noticeId, ModelMap modelMap, ActionValueAssistant actionValueAssistant){
		NoticeBoard notice=noticeBoardService.findNoticeById(noticeId);
		modelMap.put("notice", notice);
		modelMap.put("currentPageViews", notice.getPageViews());
		actionValueAssistant.addToModelMap(modelMap);
		return "website/noticeDetail";
	}
	
	
}
